package ibps;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.itextpdf.text.DocumentException;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrFormatter;
import ibps.entity.BpmExec;
import ibps.entity.Bx;
import ibps.entity.BxMx;
import ibps.entity.LiuchengHis;
import ibps.entity.Qj;
import ibps.entity.User;
import ibps.service.IBpmExecService;
import ibps.service.IBxMxService;
import ibps.service.IBxService;
import ibps.service.ILcHisService;
import ibps.service.IQjService;
import ibps.service.IUserService;
import ibps.utils.FkUtil;
import ibps.utils.PathUtil;
import ibps.utils.PdfUtil;
import lombok.AllArgsConstructor;
import lombok.Data;

@EnableScheduling
@SpringBootApplication
public class IbpsPdfApplication {

	public static void main(String[] args) {
		SpringApplication.run(IbpsPdfApplication.class, args);
	}
	
	@Autowired
	ILcHisService lcHisService;

	@Autowired
	IQjService qjService;

	@Autowired
	IBpmExecService bpmExecService;

	@Autowired
	IUserService userService;

	@Autowired
	IBxService bxService;
	@Autowired
	IBxMxService bxMxService;

	
	@Component
	public class Check {
		@Scheduled(fixedDelay = 1000 * 3)
		public void check() {
//			String process = "Process_27d5k7a";
			String process = "Process_baoxiao";
			// 1.获取所有完成的工作流
			List<LiuchengHis> his = lcHisService.findIBPS_BPM_INST_HIS(process);
			his.forEach(hi -> {
				// 2.通过工作流id获取所有的节点
				String idLiucheng = hi.getId();
				List<BpmExec> nodes = bpmExecService.findBpmByINSTId(idLiucheng);
				// 3.通过节点找出谁授权通过的,用于区分级长跟校长是谁
				String qmJz = "";// 级长签名水印
				String qmXz = "";// 校长签名水印
				for (BpmExec node : nodes) {
					String name = node.getNodeName();
					System.out.println("节点名称:" + name);
					// 4.找出账户
					Optional<User> userO = userService.findById(node.getExerIid());
					if (!userO.isPresent()) {
						continue;
					}
					User user = userO.get();
					// 5.找出签名的水印
					if ("级长审批".equals(name)) {
						qmJz = "qm1.jpg";
					} else if ("校长审批".equals(name)) {
						qmXz = "qm2.jpg";
					}
				}
				Map<String, Object> params = new HashMap<>();
				params.put("qmJz", qmJz);
				params.put("qmXz", qmXz);

				IPdf iPdf = null;
				if ("Process_27d5k7a".equals(process)) {
					File pdf = new File(StrFormatter.format("target/{}.pdf", hi.getSubject().replace(":", "")));
					iPdf = new PdfQj(idLiucheng, pdf, params);
				} else if ("Process_baoxiao".equals(process)) {
					File pdf = new File(StrFormatter.format("target/{}.pdf", hi.getSubject().replace(":", "")));
					iPdf = new PdfBx(hi.getBiz_key(), pdf, params);
				}
				try {
					iPdf.gen();
				} catch (IOException | DocumentException | com.lowagie.text.DocumentException e) {
					e.printStackTrace();
				}
			});
		}
	}
	
	
	@Data
	@AllArgsConstructor
	public class PdfBx implements IPdf {
		// 流程id
		private String bizKey;
		// 要生成pdf的路径
		private File pdf;
		private Map<String, Object> params;
		
		@Override
		public void gen() throws IOException, DocumentException, com.lowagie.text.DocumentException {
			Bx bx = bxService.findById(bizKey).get();
			params.put("bx", bx);
			List<BxMx> mxs = bxMxService.findMxByWaijianId(bx.getId());
			List<String> mxsStr = new ArrayList<>();
			mxs.forEach(mx -> {
				mxsStr.add(StrFormatter.format("<tr><td colspan='2'>出发地点:{} 出发日期:{} 到达地点:{} 达到日期:{} 人数:{} 交通工具:{} 交通费金额:{} 餐费天数:{} 餐费金额:{} 其他补助:{} 天数:{} 金额:{} 小计:{}</td></tr>", mx.getChuFadiDain(), mx.getChufaRiqi(),
						mx.getDaoDaDidian(), mx.getDaoDaRiqi(), mx.getRenShu(), mx.getJiaoTongGongju(), mx.getJiaoTongMoney(), mx.getCanFeiTianShu(), mx.getCanFieMoney(), mx.getQiTaBuzhu(),
						mx.getTianshu(), mx.getMoney(), mx.getXiaoji()));
			});
			String join = CollUtil.join(mxsStr, " ");
			System.out.println(join);
			params.put("mx", join);

			String fkTemplatePath = "template_bx.html";
			String content = new FkUtil(params, fkTemplatePath).render();
			String picPath = "file:/" + PathUtil.getCurrentPath() + "/logo.png";

			new PdfUtil(pdf, picPath).createPdf(content);
		}
	}

	@Data
	@AllArgsConstructor
	public class PdfQj implements IPdf {
		// 流程id
		private String idLiucheng;
		// 要生成pdf的路径
		private File pdf;
		private Map<String, Object> params;

		public void gen() throws IOException, DocumentException, com.lowagie.text.DocumentException {
			List<Qj> qjs = qjService.findQjByBPM_INST_HIS(idLiucheng);
			if (qjs.size() == 0) {
				System.out.println();
			}
			Qj qj = qjs.get(0);
			System.out.println(qj);

			params.put("qj", qj);

			String fkTemplatePath = "template_qj.html";
			String content = new FkUtil(params, fkTemplatePath).render();
			String picPath = "file:/" + PathUtil.getCurrentPath() + "/logo.png";

			new PdfUtil(pdf, picPath).createPdf(content);
		}
	}

	public interface IPdf {
		void gen() throws IOException, DocumentException, com.lowagie.text.DocumentException;
	}
}

